/********************************************************************/
/*  Copyright (C) School of Software Engineering of USTC, 2012                               
/*                                                                                                                       
/*  FILE NAME: list.c                                                                                          
/*  PRINCIPAL AUTHOR: SA12226114(Xin Zhu), SA12226139(Dan Shu)                    
/*  SUBSYSTEM NAME:                                                                                     
/*  MODULE NAME: list                                                                                    
/*  LANGUAGE: C                                                                                                 
/*  TARGET ENVIRONMENT: Linux                                                                          
/*  DATE OF FIRST RELEASE: 2012-12-20                                                             
/*  DESCRIPTION: Impement of Abstract Interface for Tokyo Cabinet DB API              
/********************************************************************/


#include "list.h"


/*****************************************************************
Function Name: 
Description: 
Parameter:  
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-12-20
******************************************************************/
void __list_add(struct list_head *new, struct list_head *prev, struct list_head *next)
{
    next->prev = new;
    new->next = next;
    new->prev = prev;
    prev->next = new;
}


/*****************************************************************
Function Name: list_add
Description: add a new entry. Insert a new entry after the specified head.
                  This is good for implementing stacks.
Parameter:  
@new: new entry to be added
@head: list head to add it after

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-12-20
******************************************************************/
void list_add(struct list_head *new, struct list_head *head)
{
    __list_add(new, head, head->next);
}


/*****************************************************************
Function Name: list_add_tail
Description: add a new entry. Insert a new entry before the specified head.
                  This is useful for implementing queues.
Parameter:  
@new: new entry to be added
@head: list head to add it before

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-12-20
******************************************************************/
void list_add_tail(struct list_head *new, struct list_head *head)
{
    __list_add(new, head->prev, head);
}


/*****************************************************************
Function Name: 
Description: Delete a list entry by making the prev/next entries. point to each other.
                  This is only for internal list manipulation where we know 
                  the prev/next entries already!
Parameter:  
Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-12-20
******************************************************************/
void __list_del(struct list_head * prev, struct list_head * next)
{
    next->prev = prev;
    prev->next = next;
}

 
/*****************************************************************
Function Name: list_del
Description: deletes entry from list. Note: list_empty on entry does not return true after this, 
                  the entry is in an undefined state.
Parameter:  
@entry: the element to delete from the list.

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-12-20
******************************************************************/
void list_del(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    //entry->next = LIST_POISON1;
    //entry->prev = LIST_POISON2;
}


/*****************************************************************
Function Name: 
Description: deletes entry from list and reinitialize it.
Parameter:  
@entry: the element to delete from the list.

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-12-20
******************************************************************/
void list_del_init(struct list_head *entry)
{
    __list_del(entry->prev, entry->next);
    INIT_LIST_HEAD(entry);
}


/*****************************************************************
Function Name: 
Description: tests whether a list is empty
Parameter:  
@head: the list to test.

Author: SA12226114(Xin Zhu), SA12226139(Dan Shu)
Date: 2012-12-20
******************************************************************/
int list_empty(const struct list_head *head)
{
    return head->next == head;
}



